<?php
/*
 * 公共控制器
 */
namespace app\admin\controller;
use app\common\controller\Base;

use think\Config;
use think\Request;
use think\Db;
use think\Session;
use shu\Auth;

class Comm extends Base{
	
    public $USER;
	
	public function _initialize()
	{		
        $request = Request::instance();
        config(setting());

		if (!config("my.COOKIE_SALT")) {
			$this->error('请配置COOKIE_SALT信息');
		}
        /**
         * 当是login控制器里就不跳转
         */
        if (in_array($request->controller(), array("Login"))) {
            return true;
        }
        //检测是否登录
        $flag =  $this->check_login();
        $url = 'Login/index';
        if (!$flag) {
            $this->redirect($url);
            exit(0);
        }
        $m = db();
        $prefix = config('database.prefix');
        $UID = $this->USER['uid'];
		$userinfo=Db::name('auth_group')->alias('g')->join('auth_group_access a','g.id = a.group_id','left')->where('a.uid',$UID)->select();
        $Auth = new Auth();
        $allow_controller_name = array('Upload','Loginout','Index');//放行控制器名称
        $allow_action_name = array();//放行函数名称
        if ($userinfo[0]['group_id'] != 1 && !$Auth->check($request->controller() . '/' . $request->action(),$UID) && !in_array($request->controller(), $allow_controller_name) && !in_array($request->action(),$allow_action_name)){
            $this->error('没有权限访问本页面!');
        }

        $user = member(intval($UID));
        $this->assign('user', $user);

		//左边栏输出pid
		$pid=input('param.pid');
        if($pid){
            $dd='s.cs='.$pid;
        }else{
            $dd="";
        }
		//替换一些控制器和方法名称
		$controller_name = $request->controller() =='Cache' ? "index" : $request->controller();
        $current_action_name =  $request->action() == 'edit' ? "index" : $request->action();
		
		//输出选中栏目和面包屑
		$zz=$controller_name.'/'.$current_action_name;		
		$current=Db::name('auth_rule')->alias('s')->field(['s.id','s.title','s.name','s.tips','s.pid','p.pid' => 'ppid','p.title' => 'ptitle'])->join('auth_rule p','p.id = s.pid','left')->where('s.name',$zz)->where($dd)->select();
		$this->assign('current', $current[0]);
		//echo Db::name('auth_rule')->getLastSql();die;
		
        $menu_access_id = $userinfo[0]['rules'];
        if ($userinfo[0]['group_id'] != 1){
            $menu_where = "AND id in ($menu_access_id)";
        }else{
            $menu_where = '';
        }
        $menu = db('auth_rule')->field('id,title,pid,name,icon,cs')->where("islink=1 $menu_where ")->order('o ASC')->select();
        $menu = $this->getMenu($menu);
        $this->assign('menu', $menu);
	}


    /**
     * @return bool
     */
    public function check_login(){
        $request = Request::instance();
        $flag = false;
        $salt = config("my.COOKIE_SALT");
        $ip = $request->ip();
        $ua = $_SERVER['HTTP_USER_AGENT'];
        $auth = cookie('auth');
        cookie('auth', $auth, 3600); //设置登录60分钟过期，刷新后重计时
        $uid = session('uid');
        if ($uid) {
            $user = db('member')->where(array('uid' => $uid))->find();
            if ($user) {
//              if ($auth ==  password($uid.$user['user'].$ip.$ua.$salt)) {   //客户IP有CDN IP时时在变,不能加入IP
                if ($auth ==  password($uid.$user['user'].'s1h0u'.$ua.$salt)) {
                    $flag = true;
                    $this->USER = $user;
                }
            }
        }
        return $flag;
    }

    //解析多级分类
    protected function getMenu($items, $id = 'id', $pid = 'pid', $son = 'children')
    {
        $tree = array();
        $tmpMap = array();
        //修复父类设置islink=0，但是子类仍然显示的bug @感谢linshaoneng提供代码
        foreach( $items as $item ){
            //if( $item['pid']==0 ){  //只判断到二级目录，三级或以上目录将不显示
                $father_ids[] = $item['id'];
            //}
            $tmpMap[$item[$id]] = $item;
        }
        foreach ($items as $item) {
            if( $item['pid']<>0 && !in_array( $item['pid'], $father_ids )){
                continue;
            }
            if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
            } else {
                $tree[] = &$tmpMap[$item[$id]];
            }
        }
        return $tree;
    }
	








}